<?php

namespace app\userweb\controller;

use \think\Db;
use think\Session;
use think\Config;
use think\Controller;

class Shop
{
    public function index(){

        return view('index');
    }

    /**
     * 购货(搜索)
     * @return json
     */
    public function buyProduct() {
        try {
            $cateId = input('cate_id', 0, 'intval'); //分类id
            $typeId = input('type_id', 0, 'intval'); //种类id
            $brandId = input('brand_id', 0, 'intval'); //品牌id
            $smallTypeId = input('small_type_id',0,'intval');//小类id
            $num = input('num', 0); //购买数量
            //设置搜索条件
            $where = self::getWhere($cateId, $typeId, $brandId, $num,$smallTypeId);
            //根据搜索条件搜索列表数据
            $list = self::getProductList($where);
            //分页对象转换数组
            $arr = $list->toArray();
            $data = $arr['data'];
            //将商品分配给自己所在的商家
            $result = self::getProductSupplier($data);
            $res = config('code.success');
            $res['data'] = $result;
        } catch (\Exception $ex) {
            $res = config('code.lose');
            $res['data'] = [];
        }
        return outMsg($res['status'], $res['msg'], $res['data']);
    }

    /**
     * 历史报价单
     * @return json
     */
    public function historyList() {
        try {
            $user = self::isLogin();
            //用户订单信息
            $orders = self::getOrderListByUserId($user['id']);
            $arr = $orders->toArray();
            $order = $arr['data'];
            //用户订单商品信息
            $goods = self::getGoodsListByUserId($user['id']);
            //将商品归入所属订单
            $list = self::getOrderHistory($order, $goods);
            $res = config('code.success');
            $res['data'] = $list;
        } catch (\Exception $ex) {
            $res = config('code.lose');
            $res['data'] = [];
        }
        return outMsg($res['status'], $res['msg'], $res['data']);
    }
    
}